﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using TimKen.DataEntity;

namespace TimKen.Web.User
{
    public partial class Edit : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (CurrentUserName == null || CurrentUserName == "")
                {
                    Alert("您必须登录！", "../Index.aspx");
                }
                BindResources();
                BindWebUserInfo();
            }
        }

        private void BindWebUserInfo()
        {
            WebUserManager manager = new WebUserManager();
            WebUser webUser = manager.GetModel(CurrentUserName);
            if (webUser != null)
            {
                this.lbUserName.Text = webUser.UserName;
                this.txtCompany.Text = webUser.Company;
                this.txtEmail.Text = webUser.Email;
                this.txtNickName.Text = webUser.NickName;
                this.txtPhone.Text = webUser.Phone;
                this.txtRealName.Text = webUser.RealName;
            }
            else
            {
                btnUpdate.Visible = false;
                Response.Redirect("/User/UserAccountEdit.aspx");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            WebUserManager manager = new WebUserManager();
            TimKen.DataEntity.WebUser webuser = manager.GetModel(CurrentUserName);
            if (webuser == null)
            {
                Alert(GetLangString("NoAllowUpdateUserInfo"));
            }
            else
            {
                webuser.NickName = txtNickName.Text;
                webuser.Company = txtCompany.Text;
                webuser.Email = txtEmail.Text;
                webuser.Phone = txtPhone.Text;
                webuser.RealName = txtRealName.Text;
                if (txtPassword.Text != "")
                {
                    webuser.Password = txtPassword.Text;
                }
                try
                {
                    manager.Update(webuser);
                    Alert(GetLangString("SaveSuccessful"));
                }
                catch
                {
                    Alert(GetLangString("UpdateError"));
                }
            }
        }

        protected void BindResources()
        {
            btnUpdate.Text = GetLangString("Update");
        }

    }
}
